<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>
    // 报社
    class Newspaper {
        constructor() {
            // 订阅列表
            this.readers = []
        }

        // 添加读者
        addReader(reader) {
            this.readers.push(reader)
        }

        // 移除读者
        removeReader(reader) {
            this.readers = this.readers.filter(person => person !== reader)
        }

        // 通知读者
        notifyReader(reader) {
            if (reader) return reader.take()
            this.readers.forEach(person => person.take())
        }
    }

    // 读者
    class Reader {
        constructor(name) {
            this.name = name
        }

        take() {
            console.log(`${this.name}已经收到了报纸！`)
        }
    }

    const newspaper = new Newspaper()
    const reader = new Reader('foo')
    const reader2 = new Reader('bar')
    newspaper.addReader(reader)
    newspaper.addReader(reader2)
    newspaper.removeReader(reader)
    newspaper.notifyReader()
</script>

</body>
</html>
